<template>
  <a-drawer
    title="新增黑名单"
    :maskClosable="false"
    width=650
    placement="right"
    :closable="false"
    @close="onClose"
    :visible="banAddVisiable"
    style="height: calc(100% - 55px);overflow: auto;padding-bottom: 53px;">
    <a-form :form="form">
      <a-form-item label="类型" v-bind="formItemLayout">
        <a-select :allowClear="false" v-model="banList.type" @change="handleTypeChange">
          <a-select-option value="appkey">APPKEY</a-select-option>
          <a-select-option value="sn">SN</a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item label='黑名单账户' v-bind="formItemLayout">
        <a-input v-model="banList.account"
                 v-decorator="['account',
                   {rules: [
                    { required: true, message: '不能为空'},
                    { max: 50, message: '长度不能超过50个字符'}
                  ]}]"/>
      </a-form-item>
    </a-form>
    <div class="drawer-bootom-button">
      <a-popconfirm title="确定放弃编辑？" @confirm="onClose" okText="确定" cancelText="取消">
        <a-button style="margin-right: .8rem">取消</a-button>
      </a-popconfirm>
      <a-button @click="handleSubmit" type="primary" :loading="loading">提交</a-button>
    </div>
  </a-drawer>
</template>
<script>
const formItemLayout = {
  labelCol: { span: 4 },
  wrapperCol: { span: 18 }
}
export default {
  name: 'BanAdd',
  props: {
    banAddVisiable: {
      default: false
    }
  },
  data () {
    return {
      loading: false,
      formItemLayout,
      form: this.$form.createForm(this),
      banList: {}
    }
  },
  methods: {
    handleTypeChange (key) {
      this.banList.type = key || undefined
    },
    reset () {
      this.loading = false
      this.banList = {}
      this.form.resetFields()
    },
    onClose () {
      this.reset()
      this.$emit('close')
    },
    handleSubmit () {
      this.form.validateFields((err, values) => {
        if (!err) {
          let banList = this.form.getFieldsValue()
          banList.type = this.banList.type
          this.$post('ban', {
            ...banList
          }).then(result => {
            if (result.data.code === '1') {
              this.reset()
              this.$emit('success')
            } else {
              this.$message.error('新增失败')
            }
          }).catch((data) => {
            this.loading = false
            this.$message.error('新增失败')
          })
        }
      })
    }
  }
}
</script>
